<?php
namespace Home\Controller;
use Common\Controller\FirstendController;
class TagController extends FirstendController {
	public function _initialize() {
        parent::_initialize();
        $this->_mod = D('Items');
        $this->_cate_mod = D('ItemsCate');
		$items_likes =  M('ItemsLike')->field('item_id')->where(array('uid'=>$this->visitor->info['id']))->select();
		if(is_array($items_likes)){
			$like_ids=array();
			foreach($items_likes as $like){
				 $like_ids[]=$like['item_id'];
			}
		}
		$this->assign('like_ids', $like_ids);
    }

	public function _empty($name){
		if($name){
			$this->so($name);
		}else{
			$this->_404();
		}
	}
 
	public function so($k) {
		$sort	= I('sort', 'hot', 'trim'); //排序
		$status = I('status', 'all', 'trim'); //排序
		$cid	= I('cid','', 'intval');
		$order	= 'ordid asc ,id desc';
		switch ($sort) {
            case 'new':
                $order.= ', coupon_start_time DESC';
                break;
            case 'price':
                $order.= ', price DESC';
                break;
        }
		switch ($status) {
            case 'all':
                $where['status']="underway";
                break;
            case 'underway':
                $where['status']="underway";
                break;
			case 'sellout':
				$where['status']="sellout";
				break;
        }
		if($k){
			$where['title'] = array('like', '%' . $k . '%');
			$this->assign('k',$k);
		}

		$where['pass'] = '1';
		$where['isshow'] = '1';
		$index_info['sort']=$sort;
		$index_info['status']=$status;
		$page_size = 120;
        $p = I('p',1, 'intval'); //页码
		$index_info['p']=$p;
        $start = $page_size * ($p - 1) ;
        $item_mod = M('Items');
        $items_list = $item_mod->where($where)->order($order)->limit($start . ',' . $page_size)->select();
		$items = array();
		if(!$items_list){$this->redirect(C('itk_site_url'));}
		foreach($items_list as $key=>$val){
			$items[$key]			= $val;
			$items[$key]['class']	= $this->_mod->status($val['status'],$val['coupon_start_time'],$val['coupon_end_time']);
			$items[$key]['zk']		= round(($val['coupon_price']/$val['price'])*10, 2); 
			if($items[$key]['click_url']){
				if(strlen($items[$key]['click_url'])<20){
					$items[$key]['click_url'] = '';
				}
			}
			if(!$val['click_url']){
				$items[$key]['click_url']	=U('Jump/index',array('id'=>$val['id']));
			}
			if($val['coupon_start_time']>time()){
				$items[$key]['click_url']	=U('Item/index',array('id'=>$val['id']));
			}
		}
		$this->assign('items_list', $items);
		$this->assign('index_info',$index_info);
		$count = $item_mod->where($where)->count();
        $pager = $this->_pager($count, $page_size);
        $this->assign('page', $pager->kshow());
		$this->assign('total_item',$count);

		if (false === $cate_list = F('cate_list')) {
            $cate_list = D('ItemsCate')->cate_cache();
        }
		$this->assign('cate_list', $cate_list); //分类


        $this->assign('nav_curr', 'index');
    
		$page_seo=array(
			'title' => $k.'【品牌 价格 图片 新款】，淘宝网天猫'.$k.'1折特卖包邮 - '.C('itk_site_name'),
			'keywords' => $k,
			'description' => C('itk_site_name').$k.'特卖专场，最新最全'.$k.'打折促销，所有商品人工质检，质量保证，全场包邮！',
        );
		$this->assign('page_seo', $page_seo);
        $this->display('index');
    }
    
    public function index() {
    	$p		= I('p',1 ,'intval'); //页码
    	$order	= 'id desc';
    	$where['pass'] = '1';
    	$page_size = 510;
    	$p = I('p',1, 'intval'); //页码
    	$index_info['p']=$p;
    	$start = $page_size * ($p - 1) ;
    	$item_mod = M('Tags');
    	$items_list = $item_mod->where($where)->order($order)->limit($start . ',' . $page_size)->select();
    	$items = array();
    	foreach($items_list as $key=>$val){
    		$items[$key]			= $val;
    	}
    	$this->assign('items_list', $items);
    	$this->assign('index_info',$index_info);
    	$count = $item_mod->where($where)->count();
    	$pager = $this->_pager($count, $page_size);
    	$this->assign('page', $pager->jshow());
    	$this->assign('total_item',$count);
    	$this->assign('nav_curr', 'tag');
    	$this->_config_seo(C('itk_seo_config.tag_index'));
    	$this->assign('pager','index');
    	$this->display('index');
    }
    
    public function view() {
    	import("ITK.Itklib.itaoke_https");
    	import("ITK.ORG.Pinyin");
    	
    	$id = I('k', '','trim');
    	$item_mod = M('Tags');
    	$item = $item_mod->where(array('name' => $id))->find();
    	$k = $item['name'];
    	$tagid = $item['id'];
    	$k = urlencode($k);
    	if(C('itk_tag_auto')==1){
    		$autonum = C('itk_tag_auto_num');
    		$tagurl = "http://suggest.taobao.com/sug?code=utf-8&q=".$k;
    		$itaoke_https = new \itaoke_https();
    		$itaoke_https->fetch($tagurl);
    		$sources = $itaoke_https->results;
    		$tdata = json_decode($sources,true);
    		$tinfo = array();
    		for($i=0;$i<$autonum;$i++){
    			$tinfo['name'] =  $tdata['result'][$i][0];
    			$pinyin = new \pinyin();
    			$tinfo['ename'] = $pinyin -> getAllPY($tinfo['name']);
    			$tinfo['ename'] = str_replace(" ","",$tinfo['ename']);
    			if (!$this->_mod->where(array('ename'=>$tinfo['ename']))->count()) {
    				$this->_mod->add($tinfo);
    			}
    		}
    		$title = $tdata['result'][0][0].','.$tdata['result'][1][0].','.$tdata['result'][2][0].','.$tdata['result'][3][0].','.$tdata['result'][4][0].','.$tdata['result'][5][0].','.$tdata['result'][6][0].','.$tdata['result'][7][0].','.$tdata['result'][8][0].','.$tdata['result'][9][0];
    	}
    
    	$page_size = 60;
    	$p = $this->_get('p', 'intval', 1);
    	if($p%2 == 0){
    		$ppage=0;
    		$pageNav='true';
    	}else{
    		$ppage = ($p+1)/2;
    		$pageNav='false';
    	}
    	$taodianjin = C('itk_taojindian_html');
    	if(strpos($taodianjin,'text/javascript')){
    		$pid = get_word($taodianjin,'pid: "','"');
    	}else{
    		$pid = $taodianjin;
    	}
    
    	$sourl = "http://ai.taobao.com/search/getItem.htm?_tb_token_=4fx6Pb6Bvqn&__ajax__=1&pid=".$pid."&unid=199&key=".$k."&page=".$p."&pageSize=60&ppage=".$ppage."&maxPageSize=200&neednav=1&npx=100&pageNav=".$pageNav."&sourceId=search&specialCount=6&target=item";
    	$itaoke_https = new \itaoke_https();
    	$itaoke_https->fetch($sourl);
    	$source = $itaoke_https->results;
    	if(!$source){
    		$source = file_get_contents($sourl);
    	}
    	$data = json_decode($source,true);
    	$info = array();
    	if($source){
    		for($i=0;$i<60;$i++){
    			$info['title'] = strip_tags(htmlspecialchars_decode($data['result']['auction'][$i]['description']));
    
    			$info['title']  = str_replace("&lt;em&gt;", "", $info['title']);
    			$info['title']  = str_replace("&lt;/em&gt;", "", $info['title']);
    			$info['pic_url'] =$data['result']['auction'][$i]['originalPicUrl'];
    			$info['num_iid']= $data['result']['auction'][$i]['itemId'];
    			$info['intro'] = $data['result']['auction'][$i]['comment'];
    			$info['cu'] = $data['result']['auction'][$i]['itemLocation'];
    			$info['coupon_price']= $data['result']['auction'][$i]['realPrice'];
    			$info['price']= $data['result']['auction'][$i]['price'];
    			$info['volume'] = $data['result']['auction'][$i]['saleCount'];
    			$info['click_url']= $data['result']['auction'][$i]['clickUrl'];
    			$info['type']= $data['result']['auction'][$i]['userType'];
    			$info['coupon_start_time']= time();
    			if($info['type']=='0'){
    				$info['shop_type'] = C;
    			}else{
    				$info['shop_type'] = B;
    			}
    			$info['zk'] = round(($info['coupon_price']/$info['price'])*10, 1);
    
    			$result['item_list'][]=$info;
    		}
    	}
    	$taobaoke_item_list = $result['item_list'];
    	foreach ($taobaoke_item_list as $key => $vol) {
    		$taokeitem[$key] = $vol;
    	}
    	$this->assign('taobaoke_item_list', $taokeitem);
    	$this->assign('index_info',$index_info);
    	$this->assign('tinfo',$tinfo);
    	$this->assign('title',$title);
    	$this->assign('item',$item);
    	$this->assign('tagid',$tagid);
    	$count = 1000;
    	$pager = $this->_pager($count, $page_size);
    	$this->assign('page', $pager->jshow());
    	$this->_config_seo(C('itk_seo_config.tag'), array(
    		'tagname' => $title,
    		'title' => $item['name'],
    		'keywords'=>$item['name'].','.$title,
    		'description'=>"为您提供淘宝".$item['name']."相关品牌信息，".$item['name']."哪个牌子好，价格怎么样，以及".$item['name']."十大品牌排行榜资讯信息。",
    	));
    	$this->display('view');
    }
}